<template>
  <div class="container">
    <page-header
      :title="$t('help_helpCentre')"
      :setClick="pageType == 2"
      @click="pageType = 1"
    >
      <img
        src="@/assets/img/help/ic_customer.svg"
        alt=""
        style="width: 44px; height: 44px;"
        @click="
          showPopup = true
          isShowWechat = false
        "
      />
    </page-header>
    <div class="help_div" v-show="pageType == 1">
      <div class="help_top">
        <div style="margin-right:12px;">
          <div class="help_top_title">{{ $t('help_hello') }}</div>
          <div class="help_top_content">{{ $t('help_pbHappyHelp') }}</div>
        </div>
        <div
          style="display: flex; align-items: center;justify-content: flex-end;min-width:90px;"
          @click="toFeedback"
        >
          <span class="help_top_feedback">{{ $t('help_feedback') }}</span>
          <img
            style="width: 22px; height: 22px;"
            src="@/assets/img/help/Collect.svg"
          />
        </div>
      </div>
      <div class="merchant_top">
        <div
          class="merchant_top_title"
          :class="{ merchant_top_active: item.value == activeValue }"
          v-for="item in tabList"
          :key="item.value"
          @click="changeValue(item.value)"
        >
          <div class="merchant_top_title_label">
            {{ item.label }}
          </div>
        </div>
      </div>
      <empty-data v-show="faqsList.length == 0"></empty-data>
      <div
        class="wallet_deposit_history"
        v-for="item in faqsList"
        :key="item.id"
        @click="faqDetail(item)"
      >
        <div class="wallet_deposit_history_text">{{ item.title }}</div>
        <div style="width: 48px; height: 48px;">
          <img
            style="width: 48px; height: 48px;"
            src="@/assets/img/help/porfile_ic_right@2x.png"
          />
        </div>
      </div>
    </div>

    <div class="help_detail" v-show="pageType == 2">
      <div class="help_title">{{ faqDetails.title }}</div>
      <div class="help_content">{{ faqDetails.content }}</div>
    </div>

    <van-popup
      v-model="showPopup"
      round
      position="bottom"
      :style="{ height: isShowWechat ? '368px' : '260px' }"
    >
      <div
        style="padding:  0 14px; display: flex;justify-content: space-between; "
      >
        <div style="flex: 1;">
          <img
            src="@/assets/img/login/nav_ic_back_dark@2x.png"
            alt=""
            style="width: 44px;height: 44px;"
            v-show="isShowWechat"
            @click="isShowWechat = false"
          />
        </div>

        <img
          src="@/assets/img/help/ic_cancel@2x.png"
          alt=""
          style="width: 44px;height: 44px;"
          @click="showPopup = false"
        />
      </div>
      <div class="van_content" v-if="!isShowWechat">
        <div class="van_content_phone">
          <img src="@/assets/img/help/whatsapp@2x.png" alt="" />
          <span style="text-decoration: underline;" @click="toWhatsapp">{{
            whatsapp
          }}</span>
        </div>
        <div class="van_content_phone">
          <img src="@/assets/img/help/email@2x.png" alt="" />
          <span>{{ email }}</span>
          <img
            src="@/assets/img/help/ic_copy.svg"
            alt=""
            style="width: 20px;height: 20px;margin-left:6px;"
            v-show="email"
            @click="copyEmail"
          />
        </div>
        <!-- <div class="van_content_phone" v-if="wxQr">
          <img src="@/assets/img/help/wechat@2x.png" alt="" />
          <span style="text-decoration: underline;" @click="toWechat">{{
            $t('help_qrCode')
          }}</span>
        </div> -->
      </div>
      <!-- <div class="van_content" style="text-align: center;" v-else>
        <div class="van_wx">{{ $t('help_addWhitWX') }}</div>
        <van-image
          :src="imgUrl + wxQr"
          width="150"
          height="150"
          style="padding:20px 0;"
          id="qrcode"
        ></van-image>
        <div class="van_qrdown" @click="downloadQR">
          {{ $t('help_saveQR') }}
        </div>
      </div> -->
    </van-popup>
    <!-- <vue-qr ref="qrCode" text="" :logoScale="40" :size="190" :margin="10" /> -->
  </div>
</template>
<script>
import $api from '@/assets/js/api/home'
import $apis from '@/assets/js/api/login'
import share from '@/mixins/share'
import { copyText } from '@/assets/js/util'
// import { ImagePreview } from 'vant'
export default {
  mixins: [share],
  data () {
    return {
      tabList: [],
      activeValue: 1,
      pageType: 1,
      showPopup: false,
      faqDetails: {
        title: '',
        content: ''
      },
      email: '',
      whatsapp: '',
      wxQr: '',
      faqList: [],
      isShowWechat: false,
      imgUrl: ''
    }
  },
  computed: {
    faqsList () {
      return this.faqList.filter(item => item.type == this.activeValue)
    }
  },
  created () {
    let fileHost = JSON.parse(localStorage.getItem('configs')).find(
      item => item.key == 'file-host'
    )
    if (!fileHost) this.imgUrl = process.env.BASE_URL + '/'
    else this.imgUrl = fileHost.value
    this.getLang()
    this.tabList = [
      {
        value: 1,
        label: this.$t('help_rentalOrder')
      },
      {
        value: 2,
        label: this.$t('help_payment')
      },
      {
        value: 3,
        label: this.$t('help_device')
      },
      {
        value: 4,
        label: this.$t('help_others')
      }
    ]
    this.getFaqs()
    this.getConfigs()
  },
  methods: {
    toWechat () {
      this.isShowWechat = true
    },
    imgPreview () {
      ImagePreview([this.imgUrl + this.wxQr])
    },
    downloadQR () {
      // const a = document.createElement('a') // 下载的文件名
      // a.download = 'wx' // url
      // a.href = this.$refs.qrCode.$el.src // 触发点击
      // a.click()
      let a = document.createElement('a')
      a.style.display = 'none'
      a.download = 'qrcode.png'
      a.href = this.imgUrl + this.wxQr
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
    },
    //获取配置数据
    getConfigs () {
      $apis.getConfigs(true).then(res => {
        if (res.code == 0) {
          localStorage.setItem('configs', JSON.stringify(res.data))
          res.data.forEach(item => {
            if (item.key == 'email') {
              this.email = item.value
            }
            if (item.key == 'whatsapp') {
              this.whatsapp = item.value
            }
            if (item.key == 'weChant-qr-code') {
              this.wxQr = item.value
            }
          })
        }
      })
    },
    toWhatsapp () {
      window.open(`https://api.whatsapp.com/send?phone=${this.whatsapp}`)
    },
    copyEmail () {
      copyText(this.email)
    },
    toFeedback () {
      this.$router.push({
        path: '/help/feedback'
      })
    },
    faqDetail (item) {
      this.faqDetails = item
      this.pageType = 2
    },
    changeValue (val) {
      this.activeValue = val
    },
    getFaqs () {
      $api.getFaq().then(res => {
        if (res.code == 0) {
          this.faqList = res.data
        }
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  background: #f5f5f7;
  .help_div {
    width: calc(100% - 32px);
    padding: 12px 16px 10px;
    .help_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 14px;
      .help_top_title {
        font-size: 28px;

        font-weight: 600;
        text-align: left;
        color: #39404d;
        line-height: 34px;
        margin-bottom: 4px;
      }
      .help_top_content {
        font-size: 12px;

        font-weight: 500;
        text-align: left;
        color: #66666b;
        line-height: 14px;
      }
      .help_top_feedback {
        font-size: 14px;

        font-weight: 500;
        text-align: right;
        color: var(--text-color);
        line-height: 16px;
      }
    }
    .merchant_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 44px;
      background: #ececee;
      border-radius: 10px;
      margin-bottom: 14px;
      .merchant_top_title {
        height: 36px;
        flex: 1;
        margin: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-wrap: wrap;
        font-size: 12px;

        font-weight: 500;
        text-align: center;
        color: #aaaaac;
        line-height: 14px;
        .merchant_top_title_label {
          padding: 4px 10px;
          white-space: nowrap;
        }
      }
      .merchant_top_active {
        background: #ffffff;
        border-radius: 6px;
        // box-shadow: 0px 6px 10px 0px rgba(209, 101, 0, 0.877);
        color: var(--text-color);
      }
    }
    .wallet_deposit_history {
      width: 100%;
      background: #ffffff;
      border-radius: 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
      .wallet_deposit_history_text {
        font-size: 14px;

        font-weight: 500;
        text-align: left;
        color: #39404d;
        line-height: 16px;
        padding: 16px 14px;
      }
    }
  }
  .help_detail {
    width: calc(100% - 56px);
    padding: 28px;
    .help_title {
      font-size: 21px;
      font-weight: 600;
      text-align: left;
      color: #39404d;
      line-height: 25px;
      margin-bottom: 12px;
    }
    .help_content {
      font-size: 14px;
      font-weight: 400;
      text-align: left;
      color: #39404d;
      line-height: 20px;
    }
  }
  .van-popup--bottom {
    border-radius: 30px 30px 0 0;
  }
  .van_content {
    .van_content_phone {
      display: flex;
      align-items: center;
      padding: 28px 32px;
      padding-top: 0;
      img {
        width: 28px;
        height: 28px;
        margin-right: 10px;
      }
      span {
        font-size: 17px;
        font-weight: 500;
        text-align: left;
        color: var(--text-color);
        line-height: 20px;
      }
    }
    .van_wx {
      padding: 20px 28px 0 28px;
      font-size: 17px;
      font-weight: 600;
      text-align: center;
      color: #39404d;
      line-height: 20px;
    }
    .van_qrdown {
      padding: 0 28px;
      font-size: 17px;
      font-weight: 500;
      text-decoration: underline;
      text-align: center;
      color: var(--text-color);
      line-height: 20px;
    }
  }
}
</style>
